package behavioral.command.editor;

import java.util.Stack;

public class History {
    private final Stack<UndoableCommand> undoStack = new Stack<>();
    private final Stack<UndoableCommand> redoStack = new Stack<>();

    public void push(UndoableCommand command) {
        undoStack.push(command);
        redoStack.clear();
    }

    public UndoableCommand pop() {
        if (undoStack.isEmpty()) {
            return null;
        }
        UndoableCommand command = undoStack.pop();
        redoStack.push(command);
        return command;
    }

    public UndoableCommand redo() {
        if (redoStack.isEmpty()) {
            return null;
        }
        UndoableCommand command = redoStack.pop();
        undoStack.push(command);
        return command;
    }

    public boolean canUndo() {
        return !undoStack.isEmpty();
    }
}
